package pers.zh.parking.mvp.presenter;

import android.app.Application;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.utils.RxLifecycleUtils;

import java.util.List;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import javax.inject.Inject;

import pers.zh.parking.common.BaseObserver;
import pers.zh.parking.common.BaseResponse;
import pers.zh.parking.common.RxScheduler;
import pers.zh.parking.data.entity.Notice;
import pers.zh.parking.mvp.contract.NoticeManageContract;

@ActivityScope
public class NoticeManagePresenter extends BasePresenter<NoticeManageContract.Model, NoticeManageContract.View> {

    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public NoticeManagePresenter(NoticeManageContract.Model model, NoticeManageContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void loadData() {
        mModel.getNotice()
              .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .subscribe(new BaseObserver<BaseResponse<List<Notice>>>(mRootView) {
                  @Override
                  public void onSuccess(BaseResponse<List<Notice>> response) {
                      mRootView.showNotice(response.getData());
                  }
              });
    }

    public void addNotice(Notice notice) {
        mModel.addNotice(notice)
              .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .subscribe(new BaseObserver<BaseResponse>(mRootView) {
                  @Override
                  public void onSuccess(BaseResponse response) {
                      mRootView.showMessage("发布成功");
                      mRootView.updateSuccess();
                  }
              });
    }

    public void updateNotice(Notice notice) {
        mModel.updateNotice(notice)
              .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .subscribe(new BaseObserver<BaseResponse>(mRootView) {
                  @Override
                  public void onSuccess(BaseResponse response) {
                      mRootView.showMessage("修改成功");
                      mRootView.updateSuccess();
                  }
              });
    }

    public void delete(List<Notice> list) {
        mModel.deleteNotice(list)
              .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
              .compose(RxScheduler.Obs_io_main())
              .subscribe(new BaseObserver<BaseResponse>(mRootView) {
                  @Override
                  public void onSuccess(BaseResponse response) {
                      mRootView.showMessage("删除成功");
                      mRootView.deleteSuccess(list);
                  }
              });
    }
}
